package org.orbis.structure.range;

import org.orbis.constants.Constants;

public final class FloatRangeTuple{
	private float rangeFrom, rangeTo;
	private int type;

	private FloatRangeTuple(){}

	public static FloatRangeTuple greaterThan(float value){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = Float.POSITIVE_INFINITY;
		newTuple.type = Constants.RANGE_GREATER;
		return newTuple;
	}

	public static FloatRangeTuple lessThan(float value){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = Float.NEGATIVE_INFINITY;
		newTuple.rangeTo = value;
		newTuple.type = Constants.RANGE_LESSOREQUAL;
		return newTuple;
	}

	public static FloatRangeTuple equalValue(float value){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.type = Constants.RANGE_EQUAL;
		return newTuple;
	}

	public static FloatRangeTuple notEqualValue(float value){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = value;
		newTuple.rangeTo = value;
		newTuple.type = Constants.RANGE_NOTEQUAL;
		return newTuple;
	}

	public static FloatRangeTuple range(float valueFrom, float valueTo){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		if(valueFrom < valueTo){
			newTuple.rangeFrom = valueFrom;
			newTuple.rangeTo = valueTo;
		}else {
			newTuple.rangeFrom = valueTo;
			newTuple.rangeTo = valueFrom;
		}
		newTuple.type = Constants.RANGE_RANGE;
		return newTuple;
	}

	public static FloatRangeTuple notRange(float valueFrom, float valueTo){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		if(valueFrom < valueTo){
			newTuple.rangeFrom = valueFrom;
			newTuple.rangeTo = valueTo;
		}else {
			newTuple.rangeFrom = valueTo;
			newTuple.rangeTo = valueFrom;
		}
		newTuple.type = Constants.RANGE_NOTRANGE;
		return newTuple;
	}

	public static FloatRangeTuple dontCare(){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = Integer.MIN_VALUE;
		newTuple.rangeTo = Integer.MAX_VALUE;
		newTuple.type = Constants.RANGE_DONTCARE;
		return newTuple;
	}

	public static FloatRangeTuple fromMessage(float rangeFrom, float rangeTo, int type){
		FloatRangeTuple newTuple = new FloatRangeTuple();
		newTuple.rangeFrom = rangeFrom;
		newTuple.rangeTo = rangeTo;
		newTuple.type = type;
		return newTuple;
	}

	public boolean isRange(float value){
		switch(this.type){
		case Constants.RANGE_DONTCARE:
			return true;
		case Constants.RANGE_NOTEQUAL:
			return this.rangeFrom != value;
		case Constants.RANGE_NOTRANGE:
			return(value <= this.rangeFrom || this.rangeTo <= value);
		default:
			return(this.rangeFrom <= value && value <= this.rangeTo);
		}
	}

	public float rangeFrom(){
		return rangeFrom;
	}

	public float rangeTo(){
		return rangeTo;
	}

	public int type(){
		return type;
	}
}
